package com.cet.billingjob.jobs;

import com.cet.billingjob.domain.BillingData;
import com.cet.billingjob.domain.ReportData;
import com.cet.billingjob.service.PricingService;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Value;

/**
 * @author 30952
 */
public class BillingDataProcessor implements ItemProcessor<BillingData, ReportData> {

    private final PricingService pricingService;

    @Value("${spring.cellular.spending.threshold:150}")
    private double spendingThreshold;

    public BillingDataProcessor(PricingService pricingService) {
        this.pricingService = pricingService;
    }

    @Override
    public ReportData process(BillingData item) throws Exception {
        double billingTotal = item.getDataUsage() * pricingService.getDataPricing() + item.getCallDuration() * pricingService.getCallPricing() + item.getSmsCount() * pricingService.getSmsPricing();

        if (billingTotal < spendingThreshold) {
            // return null means that record will be discarded;
            return null;
        }

        return new ReportData(item, billingTotal);
    }
}
